package cn.buk.tms.eotms.controller;

import cn.buk.common.JsonResult;
import cn.buk.common.dto.CommonDto;
import cn.buk.common.sc.CommonSearchCriteria;
import cn.buk.tms.eotms.dto.PassengerDto;
import cn.buk.tms.eotms.entity.CustomerDepartment;
import cn.buk.tms.eotms.sc.SearchFlightOrderCriteria;
import cn.buk.tms.eotms.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yfdai
 */
@RestController
public class EnterpriseController extends BaseController {

  @Autowired
  private EnterpriseService enterpriseService;

  @GetMapping("/passengers")
  public CommonDto<PassengerDto> searchPassengers(HttpServletRequest request,
                                                  @RequestParam(value = "name", required = false) String name,
                                                  @RequestParam(value = "idNo", required = false) String idNo,
                                                  @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
                                                  @RequestParam(value = "pageSize", defaultValue = "25") int pageSize) {

    SearchFlightOrderCriteria sc = new SearchFlightOrderCriteria();
    sc.setName(name);
    sc.setIdno(idNo);
    sc.setPageNo(pageNo);
    sc.setPageSize(pageSize);

    List<PassengerDto> passengers = enterpriseService.searchPassengers(getUserId(request), sc);

    CommonDto<PassengerDto> commonDto = new CommonDto<>();
    commonDto.setDataList(passengers);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 乘机人信息
   */
  @RequestMapping(value = "/my/passenger/{id}")
  public PassengerDto getPassenger(HttpServletRequest request,
                                   @PathVariable("id") int id) {
    return enterpriseService.getPassengerInfo(getUserId(request), id);
  }

  @RequestMapping("/my/departments")
  public CommonDto<CustomerDepartment> searchDepartments(HttpServletRequest request,
                                                         @ModelAttribute("sc") CommonSearchCriteria sc) {
    List<CustomerDepartment> departments = enterpriseService.searchDepartments(getUserId(request), sc);

    CommonDto<CustomerDepartment> commonDto = new CommonDto<>();
    commonDto.setDataList(departments);
    commonDto.setPage(sc.getPage());

    return commonDto;
  }

  /**
   * 创建或修改企业客户的部门信息
   */
  @RequestMapping(value = "/my/department", consumes = "application/json", method = RequestMethod.POST)
  public JsonResult createDepartment(HttpServletRequest request,
                                     @RequestBody CustomerDepartment dto) {
    return enterpriseService.createOrUpdateDepartment(getUserId(request), dto);
  }
}
